﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace OneMake.Model
{
    //全网各类目热搜品牌排行对比
    public class report_qw_search_rate
    {
        public List<string> categorys { get; set; }
        public Dictionary<string, Dictionary<string, int>> values { get; set; }
        public report_qw_search_rate()
        {
            List<string> categorys = new List<string>();
            values = new Dictionary<string,Dictionary<string,int>>();
        }
    }

    public class report_qw_search_rate2
    {
        public string category { get; set; }
        //品牌
        public string band { get; set; }

        //搜索次数
        public int search_rank_times { get; set; }
    }

    partial class ReportHelper
    {
        List<string> get_report_qw_search_rate_categorys(DateTime beginTime, DateTime endTime, int shopid)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select category from mf_m_rspp where stat_datetime BETWEEN @0 AND @1 AND shopid=@2 group by category";
            IDataReader reader = db.ExecuteReader(sql, beginTime, endTime, shopid);

            List<string> categorys = new List<string>();
            categorys.Add("全网品类搜索次数");
            while (reader.Read())
            {
                string category = reader["category"].ToString();
                categorys.Add(category);
            }
            return categorys;
        }

        public report_qw_search_rate get_report_qw_search_rate(DateTime beginTime, DateTime endTime, int shopid)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = @"select category,band,sum(n_search) as search_rank_times 
                            from mf_m_rspp 
                            where category=@0 and shopid=@1 and stat_datetime BETWEEN @2 AND @3
                            group by band order by search_rank_times DESC";

            report_qw_search_rate report = new report_qw_search_rate();
            report.categorys = get_report_qw_search_rate_categorys(beginTime, endTime, shopid);
            foreach (string category in report.categorys)
            {
                IEnumerable<report_qw_search_rate2> items = db.Query<report_qw_search_rate2>(sql, category, shopid, beginTime, endTime);

                foreach (report_qw_search_rate2 item in items)
                {
                    if (item.category == null)
                        continue;

                    if (!report.values.ContainsKey(item.band))
                        report.values.Add(item.band, new Dictionary<string, int>());

                    Dictionary<string, int> row = report.values[item.band];
                    row.Add(item.category, item.search_rank_times);
                }
            }

            return report;
        }
    }
}
